#ifndef __CLI_PARSE__TEXTURES_H__
#define __CLI_PARSE__TEXTURES_H__

#include "structs.h"
#include "resource.h" // Error Messages

#define GAME_ERROR(X) GameError(X)

#include "../../Epsilon/Epsilon3D/OpenGL.h"

class eFile;

namespace Underlight
{
    /**
   * Manages all of the Underlight wall / floor textures required
   *  by cli_parse.
   *
   *  * Use Underlight::textures-> to access the Texture Manager.
   *
  **/
  class TextureManager {
  public:
     TextureManager (void) {
       file = NULL;

       texture_headers = NULL;
       flat_headers    = NULL;

       texture_bytes = 0;
       flat_bytes    = 0;

       memset (texture_info, 0, sizeof (BITMAPINFO_4DX) * MAX_TEXTURES);
       memset (flat_info,    0, sizeof (BITMAPINFO_4DX) * MAX_FLATS);
    }

    ~TextureManager (void);

    bool LoadTextureFile (const TCHAR* filename);


    BITMAPINFO_4DX* LoadTexture      (id_t tex_id);
    BITMAPINFO_4DX* LoadFlat         (id_t flat_id);

    // Selectors
    TextureHeader*  GetTextureHeader (id_t tex_id);
    FlatHeader*     GetFlatHeader    (id_t flat_id);

    inline const BITMAPINFO_4DX& GetTexture (id_t tex_id) const {
      return texture_info [tex_id];
    }

    inline const BITMAPINFO_4DX& GetFlat    (id_t flat_id) const {
      return flat_info [flat_id];
    }

    int TexBytes  (void) { return texture_bytes; };
    int FlatBytes (void) { return flat_bytes;    };

  protected:
    eFile* file; // Keep the file open, we will read from it as needed.

  private:
    BITMAPINFO_4DX texture_info [MAX_TEXTURES];
    BITMAPINFO_4DX flat_info    [MAX_FLATS];

    TextureHeader* texture_headers;
    FlatHeader*    flat_headers;

    int            texture_bytes;
    int            flat_bytes;

    int            num_textures;
    int            num_flats;
  } extern *textures;

  enum TextureType {
    Wall,
    Flat,
    VisualEffect,
  };
};

class e3dTexture;

bool TextureFrom4DX ( BITMAPINFO_4DX*         pTex_4DX,
                      e3dTexture**            pTex,
                      Underlight::TextureType type );

bool NormalMapFrom4DX ( BITMAPINFO_4DX*         pTex_4DX,
                        e3dTexture**            pTex,
                        Underlight::TextureType type );

bool Save4DXToFile       ( BITMAPINFO_4DX*         pTex_4DX,
                           const TCHAR*            szFileName,
                           Underlight::TextureType type );

#endif /* __CLI_PARSE__TEXTURES_H__ */